var SUBMIT_FCC_FAILED = 0 ;

function AppCallback ()
{
}

AppCallback.prototype.showLatestNews = function (response)
{

	
	var feedHTML = "";
	
	//start from the second entry because the first one is just the colun headers
	
	for (var i=0; i < response.feed.entries.length; i++){
		entry = response.feed.entries[i];
		if(entry.title == "x"){
			entryArr = entry.content.split(",");
			linkurl = entryArr.pop();		//get the last element because that is the url
			linktext = entryArr.join(",");		//we split by comma so join the rest by comma
			linkurl = linkurl.replace(/linkurl: /, "");
			linktext = linktext.replace(/linktext: /, "");
			feedHTML += "<p><a target='_blank' href='"+linkurl+"'>" + linktext + "</a></p>";
			
			
		}
		
		
	}

	updateLatestNews(feedHTML);
	
}


//	'message' = "OK" for success; otherwise a descriptive failure message

AppCallback.prototype.click = function (message)
{
	
	if (message.indexOf("OK") != -1) {
		//e("savingMessage").innerHTML = "Complete!";
		//e("mod_submitMessage").innerHTML = "";
		//e("takeActionTitle").innerHTML = "Thank you.";
		//e("takeActionSkip").style.visibility = "visible";
		toggleMode();
		//hideViewport();

		var x = e("click_x").value;
		var y = e("click_y").value;

		setTimeout("refreshTile(" + x + ", " + y + ")", 5000);
		
		HAS_SUBMITTED_PIXEL = true;
		setTimeout("getLast10()", LAST10_REFRESH_INTERVAL_MS);
		
		/*
		if (!HAS_SUBMITTED_FCC) {
			//copy the fields from the click form to the fcc form
			document.getElementById ("fcc_first_name").value = document.getElementById ("first_name").value;
			document.getElementById ("fcc_last_name").value = document.getElementById ("last_name").value;
			document.getElementById ("fcc_zip").value = document.getElementById ("zip").value;
			document.getElementById ("fcc_email").value = document.getElementById ("email").value;
			
			showMenuItem("takeAction");
		}
		*/
		
		
		// Clear the fields in the reveal app submission form.
		/*
		document.getElementById ("first_name").value = "";
		document.getElementById ("last_name").value = "";
		document.getElementById ("zip").value = "";
		document.getElementById ("email").value = "";
		document.getElementById ("keep_me_updated").checked = false;
		document.getElementById ("captchaInput").value = "";
		*/
		
		// Clear the fields in the module submission form.
		/*
		document.getElementById ("mod_first_name").value = "";
		document.getElementById ("mod_last_name").value = "";
		document.getElementById ("mod_zip").value = "";
		document.getElementById ("mod_email").value = "";
		document.getElementById ("mod_keep_me_updated").checked = false;
		document.getElementById ("mod_captchaInput").value = "";
		*/
		//showNotification("Your pixel will appear in a few seconds, we thank you again for joining our effort to open the skies to wireless innovation.");
		//setTimeout("hideNotification()", 5000);
		//setClickTag("<span class='white'>Thank you </span><span class='black'>for your support.</span>");
		initClickTag();
	}

	else if (message.indexOf("DUP_BOX") != -1) {
		//e("submitMessage").style.display = "block";
		//e("savingMessage").style.display = "none";
		//showNotification("We have already received a submission to this pixel, please choose another pixel to show your support");
		
		/*
		if (HAS_SUBMITTED_PIXEL) {
			setClickTag("<span class='white'>Thank you </span><span class='black'>for doing your part to free the airwaves.</span>");
		}
		*/
		//else {
		setClickTag("<span class='black'>We have already received a submission <span class='white'>to this pixel</span>, please choose another pixel to show your support.</span>");
		//}
		toggleMode();
		//setTimeout("hideNotification()", 5000);
		//setTimeout("hideNotification()", 5000);
	}


	else if (message.indexOf("DUP_EMAIL") != -1) {
		//e("savingMessage").innerHTML = "DUP_EMAIL";
		//hideViewport();
		toggleMode();
		//e("submitMessage").style.display = "block";
		//e("savingMessage").style.display = "none";
		//showNotification("We have already received a submission from this email address. Thank you for your support, please check back often to see the progress!");
		setClickTag("<span class='black'>We have already received a submission from this email address. <span class='white'>Thank you </span>for doing your part to free the airwaves.</span>");
		//setTimeout("hideNotification()", 5000);
		//setClickTag("<span class='white'>Thank you </span><span class='black'>for your support.</span>");
		
	}
	
	else if (message.indexOf("DB_ERROR") != -1) {
		//e("savingMessage").innerHTML = "DB_ERROR";
		//hideViewport();
		toggleMode();
		//e("submitMessage").style.display = "block";
		//e("savingMessage").style.display = "none";
		setClickTag("There was an error storing your submission. Please try again later.");
		//showNotification("There was an error storing your submission. Please try again later.");
		//setTimeout("hideNotification()", 5000);
			
	}
	
	else {
		//e("savingMessage").innerHTML = "ERROR";
		//hideViewport();
		toggleMode();
		//e("submitMessage").style.display = "block";
		//e("savingMessage").style.display = "none";
		setClickTag("There was a system error during the submission process. Please try again later.");
		//showNotification("There was a system error during the submission process. Please try again later.");
		//setTimeout("hideNotification()", 5000);
	}
}

AppCallback.prototype.fccEmail = function (message)
{
		
	if (message.indexOf("OK") != -1) {
		
		if (FORWARD_USER) {
			window.location.href="thankYou.html";
			return;
		}
		
		//alert("Thanks for joining our effort to open the skies to wireless innovation.");
		alert("Thank you for doing your part to free the airwaves.");
		document.getElementById("fcc_submit_button").disabled = "disabled";
		//e("actionContainer").style.display = "none";
		//e("petitionHeader").innerHTML = "Click <span class='midGray'>a dark area to reveal the image</span>";
		//log("FCC email submission successful");
		//e("fcc_savingMessage").innerHTML = "Complete!";
		//closeMenu();
		
		//showNotification("Thank you. (And if you haven't already, please click a black area on the the screen to support our campaign.)");
		//setTimeout("hideNotification()", 5000);
		
		//e("fcc_submitMessage").style.display = "none";
		//e("fcc_savingMessage").style.display = "none";
		
		HAS_SUBMITTED_FCC = true;
		
		/*
		if (!HAS_SUBMITTED_PIXEL) {
			//copy the fields from the fcc form to the click form
			document.getElementById ("first_name").value = document.getElementById ("fcc_first_name").value;
			document.getElementById ("last_name").value = document.getElementById ("fcc_last_name").value;
			document.getElementById ("zip").value = document.getElementById ("fcc_zip").value;
			document.getElementById ("email").value = document.getElementById ("fcc_email").value;
		}
		*/
		
		// Clear the input fields
		document.getElementById ("fcc_first_name").value = "";
		document.getElementById ("fcc_last_name").value = "";
		document.getElementById ("fcc_address1").value = "";
		document.getElementById ("fcc_address2").value = "";
		document.getElementById ("fcc_city").value = "";
		document.getElementById ("fcc_state").selectedIndex = 0;
		document.getElementById ("fcc_zip").value = "";
		document.getElementById ("fcc_email").value = "";
		document.getElementById ("fcc_keep_me_updated").checked = false;
		
		//e("petitionHeader").innerHTML = "<span class='midGray'>Thank you </span>for joining our effort to open the skies to wireless innovation.";
		//startRevealApp();

	}
	
	else if (message.indexOf("DUP_EMAIL") != -1) {
		alert("We have already received a petition from this email address, we thank you again for joining our effort to open the skies to wireless innovation.");
		
		//e("savingMessage").innerHTML = "DUP_EMAIL";
		//closeMenu();
		//e("fcc_submitMessage").style.display = "block";
		//e("fcc_savingMessage").style.display = "none";
		//showNotification("We have already received a submission from this email address. Thank you for your support, please check back often to see the progress!");
		//setTimeout("hideNotification()", 5000);
	}
	
	else if (message.indexOf("DB_ERROR") != -1) {
		
		 if(SUBMIT_FCC_FAILED == 0){
                        SUBMIT_FCC_FAILED = 1;
                        setTimeout("doFCCEmail()",200);
                        return ;
                }

		alert("There was an error storing your petition, please try re-submitting the information in a few seconds.");
		//e("savingMessage").innerHTML = "DB_ERROR";
		//closeMenu();
		//e("fcc_submitMessage").style.display = "block";
		//e("fcc_savingMessage").style.display = "none";
		//showNotification("There was an error storing your submission. Please try again later.");
		//setTimeout("hideNotification()", 5000);
	}
	
	else {
		if(SUBMIT_FCC_FAILED == 0){
                        SUBMIT_FCC_FAILED = 1;
                        setTimeout("doFCCEmail()",200);
                        return ;
                }
		
		alert("There was a system error while storing your petition, please try re-submitting the information in a few seconds.");
		//e("savingMessage").innerHTML = "ERROR";
		//closeMenu();
		//e("fcc_submitMessage").style.display = "block";
		//e("fcc_savingMessage").style.display = "none";
		//showNotification("There was a system error during the submission process. Please try again later.");
		//setTimeout("hideNotification()", 5000);
	}
}

AppCallback.prototype.last10 = function (message) {
	try {
		var last10Result = eval(message);
		//log(last10Result.length);
		var len = last10Result.length
		if (len == 0) {
			return;
		}
		
		//var ids = {};
		//var maxId = 0;
		var minId = 0;
		var maxId = 0;
		var maxIndex = 0;
		
		for (var i=0; i<len; i++) {
			if (last10Result[i].id > maxId) {
				//ids[i] = last10Result[i].id;
				maxId = last10Result[i].id;
				maxIndex = i;
			}
		}
		
		//log("Max of " + maxId + " is at index: " + maxIndex);
		
		for (var i=0; i <= maxIndex; i++) {
			//ids[i] = last10Result[maxIndex - i].id;
			//log("Saving id " + last10Result[maxIndex - i].id + " to index " + i);
			createSample(i, last10Result[maxIndex-i].id, last10Result[maxIndex-i].x, last10Result[maxIndex-i].y, last10Result[maxIndex-i].size);
		}
		
		for (var i=0; i < (len - maxIndex - 1); i++) {
			//ids[maxIndex + i + 1] = last10Result[(len - i) - 1].id;
			createSample((maxIndex + i + 1), last10Result[(len - i) - 1].id, last10Result[(len - i) - 1].x, last10Result[(len - i) - 1].y, last10Result[(len - i) - 1].size);
		}

		/*
		for (var i=(len-1); i > maxIndex; i--) {
			ids[(len - 1) - i] = last10Result[i].id;
		}
		*/
		
		/*
		for (var i=0; i<len; i++) {
			log(ids[i]);
		}
		*/
		
		/*
		// Set maxId to zero to start then find the largest with this loop
		for (var j=1; j<last10Result.length; j++) {
			
			for (var i=0; i<last10Result.length; i++) {
				if (last10Result[i].id > maxId) {
					maxIds[j-1] = last10Result[i].id;
					log(maxIds[j-1]);
				}
			}
		}
		*/
		/*
		var count = 0;
		
		// For each of the samples, iterate through the result set and plot the results with 
		// highest ids first.
		while (count < last10Result.length) {
			
			for(var i=0; i<last10Result.length; i++) {
				if (last10Result[i].id == maxIds[i]) {
					//log("count: " + count + ", id: " + last10Result[i].id + ", x: " + last10Result[i].x + ", y: " + last10Result[i].y + ", size: " + last10Result[i].size);
					createSample(count, last10Result[i].id, last10Result[i].x, last10Result[i].y, last10Result[i].size);
					--maxId;
					++count;
				}
			}
			
		}
		*/
	}
	catch (e) {
		alert("last10 callback threw exception: " + e);
	}
	
	// Only get more last10s if they've submitted, prevents contention on XMLHTTPRequests
	if (HAS_SUBMITTED_PIXEL) {
		setTimeout("getLast10()", LAST10_REFRESH_INTERVAL_MS);
	}
}
